{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bottom-ivory",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mtrait\u001b[39m \u001b[36mHaircolor\u001b[39m"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trait Haircolor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "alive-brazilian",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mtrait\u001b[39m \u001b[36mIterator\u001b[39m"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trait Iterator[A] {\n",
    "  def hasNext: Boolean\n",
    "  def next(): A\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "changing-steering",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mtrait\u001b[39m \u001b[36mIterator\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mIntIterator\u001b[39m\r\n",
       "\u001b[36miterator\u001b[39m: \u001b[32mIntIterator\u001b[39m = ammonite.$sess.cmd5$Helper$IntIterator@29b0edb3\r\n",
       "\u001b[36mres5_3\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m0\u001b[39m"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trait Iterator[A] {\n",
    "    def hasnext:Boolean\n",
    "    def next():A\n",
    "}\n",
    "\n",
    "class IntIterator(to :Int) extends Iterator[Int]{\n",
    "    private var current = 0\n",
    "    override def hasnext :Boolean = current < to\n",
    "    override def next():Int = {\n",
    "        if (hasnext) {\n",
    "            val t = current\n",
    "            current +=1\n",
    "            t\n",
    "        } else 0\n",
    "    }\n",
    "}\n",
    "\n",
    "val iterator = new IntIterator(10)\n",
    "iterator.next()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "miniature-sword",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mres6\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m1\u001b[39m"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iterator.next()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "comic-cyprus",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mres16\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m6\u001b[39m"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iterator.next()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "collectible-effects",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[32mimport \u001b[39m\u001b[36mscala.collection.mutable.ArrayBuffer\u001b[39m"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import scala.collection.mutable.ArrayBuffer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "enhanced-child",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mtrait\u001b[39m \u001b[36mPet\u001b[39m"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trait Pet {\n",
    "  val name: String\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "broke-personality",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mCat\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mDog\u001b[39m"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Cat(val name :String) extends Pet\n",
    "class Dog(val name :String) extends Pet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "supposed-rescue",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mdog\u001b[39m: \u001b[32mDog\u001b[39m = ammonite.$sess.cmd19$Helper$Dog@5d340a47\r\n",
       "\u001b[36mcat\u001b[39m: \u001b[32mCat\u001b[39m = ammonite.$sess.cmd19$Helper$Cat@33c09ccb"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val dog = new Dog(\"Harry\")\n",
    "val cat = new Cat(\"Sally\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "secure-chemistry",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36manimals\u001b[39m: \u001b[32mArrayBuffer\u001b[39m[\u001b[32mPet\u001b[39m] = \u001b[33mArrayBuffer\u001b[39m()"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val animals =ArrayBuffer.empty[Pet]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "accessible-terrain",
   "metadata": {},
   "outputs": [],
   "source": [
    "animals.append(dog)\n",
    "animals.append(cat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "killing-chester",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Harry\n",
      "Sally\n"
     ]
    }
   ],
   "source": [
    "animals.foreach(pet => println(pet.name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "international-divorce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mcat1\u001b[39m: \u001b[32mCat\u001b[39m = ammonite.$sess.cmd19$Helper$Cat@32dcc92d\r\n",
       "\u001b[36mres24_1\u001b[39m: \u001b[32mArrayBuffer\u001b[39m[\u001b[32mPet\u001b[39m] = \u001b[33mArrayBuffer\u001b[39m(\n",
       "  ammonite.$sess.cmd19$Helper$Dog@5d340a47,\n",
       "  ammonite.$sess.cmd19$Helper$Cat@33c09ccb,\n",
       "  ammonite.$sess.cmd19$Helper$Cat@32dcc92d\n",
       ")"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val cat1 = new Cat(\"Sally1\")\n",
    "animals += cat1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "recognized-being",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Harry\n",
      "Sally\n",
      "Sally1\n"
     ]
    }
   ],
   "source": [
    "animals.foreach(pet => println(pet.name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "turned-sender",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
